<?php
namespace App\DBs\Admin;

use Illuminate\Support\Facades\DB;
use App\DBs\BaseDB as Base;

class MeetDB extends Base
{
    //大会

    public function __construct()
    {
    }

    //列表
    public function getIndex()
    {
        $res = DB::table($this->tMeet)->where('del',0)->get();
        if (!count($res)) {
            return array('code'=>-2,'msg'=>'没有数据');
        }
        $res = $this->jsonToArr($res);
        $items = [];
        foreach ($res as $re) {
            $img = env('DOMAIN').'/'.$re['img'];
            $items[] = [
                'id'=>$re['id'], 'title'=>$re['title'],
                'img'=>$img, 'jointime'=>$re['jointime'],
            ];
        }
        return array('code'=>0,'msg'=>'success','data'=>$items);
    }

    //一个
    public function getOne($id)
    {
        $res = DB::table($this->tMeet)->where('del',0)->where('id',$id)->first();
        if (!count($res)) {
            return array('code'=>-2,'msg'=>'没有数据');
        }
        $res = $this->jsonToArr($res);
        $img = env('DOMAIN').'/'.$res['img'];
        $items = [
            'id'=>$res['id'], 'title'=>$res['title'],
            'img'=>$img, 'jointime'=>$res['jointime'],
        ];
        return array('code'=>0,'msg'=>'success','data'=>$items);
    }

    //新增
    public function setAdd($arys)
    {
        $totime = date('Y-m-d H:i:s',time());
        $videoData = [
            'title'=>$arys['title'], 'img'=>$arys['img'],
            'jointime'=>$arys['jointime'], 'addtime'=>$totime,
        ];
        $res = DB::table($this->tMeet)->insertGetId($videoData);
        if (!$res) {
            return array('code'=>-2,'msg'=>'新增错误');
        }
        return array('code'=>0, 'msg'=>'操作成功','data'=>$res);
//        //开启事务
//        DB::beginTransaction();
//        try {
//            //提交事务
//            DB::commit();
//            return array('code'=>0, 'msg'=>'操作成功');
//        } catch (\Exception $e) {
//            //事务回滚
//            DB::rollBack(); echo $e->getMessage();
//            return array('code'=>-1, 'msg'=>'操作失败');
//        }
    }

    //修改
    public function setUpdate($id,$arys)
    {
        $res = DB::table($this->tMeet)->where('id',$id)->first();
        if (!$res) {
            return array('code'=>-2,'msg'=>'没有数据');
        }
        $res = $this->jsonToArr($res);
        if ($arys['img'] && file_exists($res['img'])) {
            unlink($res['img']);
        }
        $totime = date('Y-m-d H:i:s',time());
        $arys['addtime'] = $totime;
        $res = DB::table($this->tMeet)->where('id',$id)->update($arys);
        if (!$res) {
            return array('code'=>-2,'msg'=>'修改错误');
        }
        return array('code'=>0, 'msg'=>'操作成功','data'=>$res);
    }

    //删除
    public function setDel($id)
    {
        $totime = date('Y-m-d H:i:s',time());
        $res = DB::table($this->tMeet)->where('id',$id)->update(['del'=>1,'deltime'=>$totime]);
        if (!$res) {
            return array('code'=>-2,'msg'=>'删除错误');
        }
        return array('code'=>0, 'msg'=>'操作成功','data'=>$res);
    }

    //大会选项
    public function getOption($selid)
    {
        $res = DB::table($this->tMeet)->where('del',0)->get();
        if (!count($res)) {
            return array('code'=>-2,'msg'=>'没有数据');
        }
        $res = $this->jsonToArr($res);
        $items = [];
        foreach ($res as $re) {
            $title = $re['title'].' - '.$re['jointime'];
            $check = $re['id']==$selid ? 'Y' : 'N';
            $items[] = [
                'id'=>$re['id'], 'title'=>$title, 'checked'=>$check,
            ];
        }
        return array('code'=>0,'msg'=>'success','data'=>$items);
    }

    //主题列表
    public function getFocusIndex($meetid)
    {
        $db = DB::table($this->tFocus)->where('del',0);
        if ($meetid) $db = $db->where('meetid',$meetid);
        $res = $db->orderBy('uptime','desc')->orderBy('id','desc')->get();
        if (!count($res)) {
            return array('code'=>-2,'msg'=>'没有数据');
        }
        $res = $this->jsonToArr($res);
        $items = [];
        foreach ($res as $re) {
            $img = file_exists($re['img']) ? env('DOMAIN').'/'.$re['img'] : '';
            $items[] = [
                'id'=>$re['id'], 'title'=>$re['title'], 'detail'=>$re['detail'],
                'img'=>$img, 'starttime'=>$re['starttime'], 'meetid'=>$re['meetid'],
            ];
        }
        return array('code'=>0,'msg'=>'success','data'=>$items);
    }

    //主题新增
    public function setFocusAdd($arys)
    {
        $totime = date('Y-m-d H:i:s',time());
        $arys['addtime'] = $totime;
        $res = DB::table($this->tFocus)->insertGetId($arys);
        if (!$res) {
            return array('code'=>-2,'msg'=>'新增错误');
        }
        return array('code'=>0, 'msg'=>'操作成功','data'=>$res);
    }

    //主题获取
    public function getFocusOne($id)
    {
        $res = DB::table($this->tFocus)->where('id',$id)->first();
        if (!$res) {
            return array('code'=>-2,'msg'=>'没有数据');
        }
        $res = $this->jsonToArr($res);
//        $detail = htmlspecialchars_decode($res['detail']);
        $detail = json_decode($res['detail'],true);
        $img = file_exists($res['img']) ? env('DOMAIN').'/'.$res['img'] : '';
        $items = [
            'id'=>$res['id'], 'meetid'=>$res['meetid'],
            'title'=>$res['title'], 'detail'=>$detail,
            'img'=>$img, 'starttime'=>$res['starttime'],
        ];
        return array('code'=>0, 'msg'=>'操作成功','data'=>$items);
    }

    //主题修改
    public function setFocusUpdate($id,$arys)
    {
        $res = DB::table($this->tFocus)->where('del',0)->where('id',$id)->first();
        if (!$res) {
            return array('code'=>-2,'msg'=>'没有数据');
        }
        $res = $this->jsonToArr($res);
        if (isset($arys['img']) && $arys['img'] && file_exists($res['img'])) {
            unlink($res['img']);
        }
        $totime = date('Y-m-d H:i:s',time());
        $arys['uptime'] = $totime;
        $res = DB::table($this->tFocus)->where('id',$id)->update($arys);
        if (!$res) {
            return array('code'=>-2,'msg'=>'新增错误');
        }
        return array('code'=>0, 'msg'=>'操作成功','data'=>$res);
    }

    //主题删除
    public function setFocusDel($id)
    {
        $totime = date('Y-m-d H:i:s',time());
        $res = DB::table($this->tFocus)->where('id',$id)->update(['del'=>1,'deltime'=>$totime]);
        if (!$res) {
            return array('code'=>-2,'msg'=>'删除错误');
        }
        return array('code'=>0, 'msg'=>'操作成功','data'=>$res);
    }

    //嘉宾列表
    public function getSpeakerIndex($meetid)
    {
        $db = DB::table($this->tSpeaker)->where('del',0);
        if ($meetid) $db = $db->where('meetid',$meetid);
        $res = $db->orderBy('id','desc')->get();
        if (!count($res)) {
            return array('code'=>-2,'msg'=>'没有数据');
        }
        $res = $this->jsonToArr($res);
        $items = [];
        foreach ($res as $re) {
            $uhead = env('DOMAIN').'/'.$re['uhead'];
            $items[] = [
                'id'=>$re['id'], 'uname'=>$re['uname'],
                'udetail'=>$re['udetail'], 'uhead'=>$uhead,
            ];
        }
        return array('code'=>0,'msg'=>'success','data'=>$items);
    }

    //主题新增
    public function setSpeakerAdd($arys)
    {
        $totime = date('Y-m-d H:i:s',time());
        $arys['addtime'] = $totime;
        $res = DB::table($this->tSpeaker)->insertGetId($arys);
        if (!$res) {
            return array('code'=>-2,'msg'=>'新增错误');
        }
        return array('code'=>0, 'msg'=>'操作成功','data'=>$res);
    }

    //嘉宾删除
    public function setSpeakerDel($id)
    {
        $totime = date('Y-m-d H:i:s',time());
        $res = DB::table($this->tSpeaker)->where('id',$id)->update(['del'=>1,'deltime'=>$totime]);
        if (!$res) {
            return array('code'=>-2,'msg'=>'删除错误');
        }
        return array('code'=>0, 'msg'=>'操作成功','data'=>$res);
    }

    //资料列表
    public function getFileIndex($meetid)
    {
        $db = DB::table($this->tFile)->where('del',0);
        if ($meetid) $db = $db->where('meetid',$meetid);
        $res = $db->orderBy('id','desc')->get();
        if (!count($res)) {
            return array('code'=>-2,'msg'=>'没有数据');
        }
        $res = $this->jsonToArr($res);
        $items = [];
        foreach ($res as $re) {
            $link = env('DOMAIN').'/'.$re['link'];
            $items[] = [
                'id'=>$re['id'], 'fname'=>$re['fname'],
                'fext'=>strtoupper($re['fext']), 'link'=>$link,
            ];
        }
        return array('code'=>0,'msg'=>'success','data'=>$items);
    }

    /**
     * 资料上传
     * @param forigin 文件源名，fext文件后缀，fpath文件地址
     */
    public function setFileAdd($arys)
    {
        $totime = date('Y-m-d H:i:s',time());
        $arys['addtime'] = $totime;
        $res = DB::table($this->tFile)->insertGetId($arys);
        if (!$res) {
            return array('code'=>-2,'msg'=>'新增错误');
        }
        return array('code'=>0, 'msg'=>'操作成功','data'=>$res);
    }

    //资料新增
    public function setFileUpdate($id,$arys)
    {
        $totime = date('Y-m-d H:i:s',time());
        $arys['uptime'] = $totime;
        $res = DB::table($this->tFile)->where('id',$id)->update($arys);
        if (!$res) {
            return array('code'=>-2,'msg'=>'新增错误');
        }
        return array('code'=>0, 'msg'=>'操作成功','data'=>$res);
    }

    //资料删除
    public function setFileDel($id)
    {
        $totime = date('Y-m-d H:i:s',time());
        $res = DB::table($this->tFile)->where('id',$id)->update(['del'=>1,'deltime'=>$totime]);
        if (!$res) {
            return array('code'=>-2,'msg'=>'删除错误');
        }
        return array('code'=>0, 'msg'=>'操作成功','data'=>$res);
    }
}